# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import CrawlSpider,Rule  
from scrapy.linkextractors import LinkExtractor  
from csdnspider.items import CsdnspiderItem  

class CsdnSpider(CrawlSpider):
    name = 'csdn'
    allowed_domains = ['edu.csdn.net']
    start_urls = ['https://edu.csdn.net/courses/k/']

    item=CsdnspiderItem()

    #Rule是在定义抽取链接的规则，获取页面url
    rules = (  
        Rule(LinkExtractor(allow=(r"https://edu.csdn.net/courses/k/p[0-9]+",)), callback='parse_item',  
             follow=True),  
    ) 

    #获取该页面下的课程url地址
    def parse_item(self,response):  
        courseslist = response.css("div.course_item")  
        for course in courseslist:
            item=CsdnspiderItem()
            item['url'] = course.css("a[target='_blank']::attr(href)").extract_first()
            print("url====",item)
            yield item
